<?php
use Illuminate\Support\Facades\Log;
use Modules\Category\Models\Category;

if (!function_exists('menuTree')) {
    function menuTree($status = null, $pid = 0,$group = 'menu')
    {
        $tree = [];
        foreach (Category::select(['id','name','pid','level','status'])
        ->where('group', $group)
        ->where('pid', $pid)->orderBy('id')->get() as $cat) {
            $children = menuTree($status, $cat->id,$group);
            if ($status === null || $cat->status == $status || $children) {
                $item = $cat->toArray();
                $item['children'] = $children;
                $tree[] = $item;
            }
        }
        return $tree;
    }
}

//全部
if(!function_exists('cache_menu_all')){
    function cache_menu_all($group = 'menu'){
        // 缓存键的命名格式是'menu_tree_all_' . $group
        $cacheKey = 'menu_tree_all_' . $group;
        return cache()->rememberForever($cacheKey, function () use ($group) {
            return menuTree(null,0,$group);
        });
    }
}
if(!function_exists('cache_menu')){
    function cache_menu($group = 'menu'){
        // 根据group参数生成不同的缓存键
        $cacheKey = 'menu_tree_' . $group;
        cache()->rememberForever($cacheKey, function () use ($group) {
            return menuTree(1,$group);
        });
    }
}